<%-- 
    Document   : question-add-handler
    Created on : Feb 16, 2012, 10:50:21 AM
    Author     : PhucAn
--%>
<%@page import="util.SharingUtil"%>
<%@page import="dao.AnswerDAO"%>
<%@page import="dto.Answer"%>
<%@page import="dto.Question"%>
<%@page import="dao.QuestionDAO"%>
<%
    if (request.getParameter("submit") != null) {
        boolean result = false;
        int cateId = -1;
        try {
            cateId = Integer.parseInt(request.getParameter("cateid"));
        } catch (NumberFormatException ex) {
            SharingUtil.showMessage(session, "Invalid category");
            return;
        }
        QuestionDAO daoQuestion = new QuestionDAO();
        AnswerDAO daoAns = new AnswerDAO();
        Question q = new Question();
        String questionContent = request.getParameter("content"); //question content
        String[] answers = request.getParameterValues("answer"); //answers content
        String singleChoice = null; //hold true value for single choice question
        String[] multiChoices = null; //hold true value for multichoices question
        boolean isSingle = false; //check if a question is single choice or multichoice
        if (request.getParameter("multi-choice") != null) {
            multiChoices = request.getParameterValues("multi-ans-result");
        } else {
            singleChoice = request.getParameter("single-ans-result");
            isSingle = true;
        }
        float mark = 1;
        try {
            mark = Float.parseFloat(request.getParameter("mark"));
        } catch (NumberFormatException ex) {
        }
        q.setContent(questionContent);
        q.getCategory().setId(cateId);
        q.setLevel(Byte.parseByte(request.getParameter("level")));
        q.setMark(mark);

        for (String ans : answers) {
            Answer ansObj = new Answer();
            ansObj.setContent(ans);
            q.getAnswers().add(ansObj);
        }
        if (isSingle) {
            //in case single choice
            int index = Integer.parseInt(singleChoice);
            q.getAnswers().get(index - 1).setIsTrue(true);
            if (daoQuestion.create(q) > 0) {
                if (daoAns.create(q) > 0) {
                    result=true;
                    session.setAttribute(SharingUtil.VAR_S_ADMIN_MESSAGE, "New question is created");
                } else {
                    result=true;
                    session.setAttribute(SharingUtil.VAR_S_ADMIN_MESSAGE, "Cannot create answer for question");
                }
            } else {
                session.setAttribute(SharingUtil.VAR_S_ADMIN_MESSAGE, "Cannot create question");
            }
        } else {
            //in case multi-choices
            for (String theTrue : multiChoices) {
                int index = Integer.parseInt(theTrue);
                q.getAnswers().get(index - 1).setIsTrue(true);
            }
            if (daoQuestion.create(q) > 0) {
                if (daoAns.create(q) > 0) {
                    result=true;
                    session.setAttribute(SharingUtil.VAR_S_ADMIN_MESSAGE, "New question is added");
                } else {
                    result=true;
                    session.setAttribute(SharingUtil.VAR_S_ADMIN_MESSAGE, "Error occured when creating question");
                }
            } else {
                session.setAttribute(SharingUtil.VAR_S_ADMIN_MESSAGE, "Error. Question cannot be created");
            }
        }
        if(result){
           //save current question in session
           //redirect to update page 
        }
    }

%>
